package com.doubletick.proxy.ipidea.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubletick.common.annotation.DataScope;
import com.doubletick.common.utils.DateUtils;
import com.doubletick.common.utils.spring.SpringUtils;
import com.doubletick.proxy.ipidea.cache.ProxyAccountCache;
import com.doubletick.proxy.ipidea.cache.UsernameCache;
import com.doubletick.proxy.ipidea.domain.ProxyFlowChargeHistory;
import com.doubletick.proxy.ipidea.enums.OperateResult;
import com.doubletick.proxy.ipidea.mapper.ProxyFlowChargeHistoryMapper;
import com.doubletick.proxy.ipidea.service.IProxyFlowChargeHistoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 流量充值历史Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-16
 */
@Service
public class ProxyFlowChargeHistoryServiceImpl extends ServiceImpl<ProxyFlowChargeHistoryMapper, ProxyFlowChargeHistory> implements IProxyFlowChargeHistoryService {

    @Resource
    private UsernameCache userNameCache;

    @Override
    public boolean updateProxyIdByUserId(String proxyId, Long userId) {
        LambdaUpdateWrapper<ProxyFlowChargeHistory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ProxyFlowChargeHistory::getProxyId, proxyId);
        updateWrapper.eq(ProxyFlowChargeHistory::getUserId, userId);
        return this.update(updateWrapper);
    }

    /**
     * 查询流量充值历史列表
     *
     * @param proxyFlowChargeHistory 流量充值历史
     * @return 流量充值历史
     */

    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<ProxyFlowChargeHistory> selectProxyFlowChargeHistoryList(ProxyFlowChargeHistory proxyFlowChargeHistory) {
        proxyFlowChargeHistory.setOperateResult(OperateResult.SUCCESS);
        List<ProxyFlowChargeHistory> list = getBaseMapper().selectProxyFlowChargeHistoryList(proxyFlowChargeHistory);
        setCreateUsername(list);
        setProxyAccount(list);
        return list;
    }

    private void setProxyAccount(List<ProxyFlowChargeHistory> list) {
        list.forEach(proxyAccount -> {
            if (StrUtil.isEmpty(proxyAccount.getProxyAccount()) && StrUtil.isNotEmpty(proxyAccount.getProxyId())) {
                String account= SpringUtils.getBean(ProxyAccountCache.class).getProxyAccountByProxyId(proxyAccount.getProxyId());
                if (StrUtil.isNotEmpty(account)) {
                    proxyAccount.setProxyAccount(account);
                }
            }
        });
    }

    private void setCreateUsername(List<ProxyFlowChargeHistory> proxyFlowChargeHistories) {
        proxyFlowChargeHistories.forEach(history -> {
            history.setCreateUsername(userNameCache.getUsernameByUserId(history.getCreateUserId()));
        });
    }

    /**
     * 查询流量充值历史
     *
     * @param id 流量充值历史主键
     * @return 流量充值历史
     */
    @Override
    public ProxyFlowChargeHistory selectProxyFlowChargeHistoryById(Long id) {
        return this.getById(id);
    }


    /**
     * 新增流量充值历史
     *
     * @param proxyFlowChargeHistory 流量充值历史
     * @return 结果
     */
    @Override
    public boolean insertProxyFlowChargeHistory(ProxyFlowChargeHistory proxyFlowChargeHistory) {
        proxyFlowChargeHistory.setCreateTime(DateUtils.getNowDate());
        return this.save(proxyFlowChargeHistory);
    }

    /**
     * 修改流量充值历史
     *
     * @param proxyFlowChargeHistory 流量充值历史
     * @return 结果
     */
    @Override
    public boolean updateProxyFlowChargeHistory(ProxyFlowChargeHistory proxyFlowChargeHistory) {
        return this.updateById(proxyFlowChargeHistory);
    }

    /**
     * 批量删除流量充值历史
     *
     * @param ids 需要删除的流量充值历史主键
     * @return 结果
     */
    @Override
    public int deleteProxyFlowChargeHistoryByIds(Long[] ids) {
        return getBaseMapper().deleteByIds(Arrays.asList(ids));
    }

    /**
     * 删除流量充值历史信息
     *
     * @param id 流量充值历史主键
     * @return 结果
     */
    @Override
    public int deleteProxyFlowChargeHistoryById(Long id) {
        return getBaseMapper().deleteById(id);
    }

}
